<!DOCTYPE html>
<html lang="en" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="common :: header('编辑幻灯片')"/>
    <style>
        .layui-form-label{
            width:80px;
        }
    </style>
</head>
<body>
<form action="" class="layui-form">
    <div class="">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">分类</label>
                <div class="layui-input-block">
                    <select name="slideId">
                        <option th:each="citem : ${cateList}" th:text="${citem.name}" th:value="${citem.id}" th:selected="${citem.id} eq ${item.slideId}">
                        </option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">

                    <input autocomplete="off" class="layui-input" lay-verify="title" name="title" placeholder="请输入名称"
                           type="text" th:value="${item.title}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图片</label>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" id="ID-upload-demo-btn">
                        <i class="layui-icon layui-icon-upload"></i> 上传图片
                    </button>
                    <div style="width: 132px;">
                        <div class="layui-upload-list">
                            <input type="hidden" name="image" id="thumb-input" th:value="${item.image}">
                            <img class="layui-upload-img"  th:src="${item.image!=null?item.image:'/admin/images/upload_default.png'}" id="ID-upload-demo-img" style="width: 100%; height: 92px;">
                            <div id="ID-upload-demo-text"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">链接</label>
                <div class="layui-input-block">
                    <input autocomplete="off" class="layui-input"  name="url"
                           placeholder="请输入链接"
                           type="text" th:value="${item.url}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input checked name="status" title="禁用" type="radio" value="0" th:checked="${item.status eq 0}">
                    <input name="status" title="正常" type="radio" value="1" th:checked="${item.status eq 1}">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea class="layui-textarea" name="content" placeholder="请输入备注"
                              th:text="${item.content}"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input type="hidden" name="id" th:value="${item.id}">
                    <button class="pear-btn pear-btn-primary" lay-submit lay-filter="formDemo">保存</button>
                </div>
            </div>
        </div>
    </div>
</form>
</body>

<th:block th:include="common :: footer"/>
<script>
    layui.use(['form', 'jquery', 'layer'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let layer = layui.layer;
        //设置上传相关
        var upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#ID-upload-demo-btn',
            url: '/admin/public/upload',
            done: function(res){
                debugger;
                if(res.code == 0){
                    return layer.msg('上传失败');
                }
                $("#ID-upload-demo-img").attr("src",res.data.url);
                $("#thumb-input").val(res.data.url);
            }
        });
        form.on('submit(formDemo)', function (data) {
            $.ajax({
                url: '/admin/slideItem/editPost',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("slide-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })

</script>
</html>